
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.id {
import org.waveprotocol.wave.model.util.Constants;

public class LongIdSerialiser implements IdSerialiser {
	/** Separates a wave id from a wavelet id in serialised form. */
	public static var PART_SEPARATOR:String= '!';
	public static var INSTANCE:IdSerialiser= new LongIdSerialiser();
	
	
	public function serialiseWaveId(waveId:WaveId):String {
		return waveId.getDomain() + PART_SEPARATOR + waveId.getId();
	}
	
	
	public function serialiseWaveletId(waveletId:WaveletId):String {
		return waveletId.getDomain() + PART_SEPARATOR + waveletId.getId();
	}
	
	
	public function deserialiseWaveId(serialisedForm:String):WaveId {
		var parts:Array= Constants.DEFAULT_ESCAPER.splitWithoutUnescaping(
			PART_SEPARATOR, serialisedForm);
		if (parts.length != 2) {
			throw new Error("Unable to deserialise the long wave id: " +
				serialisedForm + ". The wave id need to look like <domain>" + PART_SEPARATOR + "<id>");
		} else {
			return new WaveId(parts[0], parts[1]);
		}
	}
	
	
	public function deserialiseWaveletId(serialisedForm:String):WaveletId {
		var parts:Array= Constants.DEFAULT_ESCAPER.splitWithoutUnescaping(
			PART_SEPARATOR, serialisedForm);
		if (parts.length != 2) {
			throw new Error("Unable to deserialise the long wavelet id: " +
				serialisedForm + ". The wavelet id need to look like <domain>" + PART_SEPARATOR + "<id>");
		} else {
			return new WaveletId(parts[0], parts[1]);
		}
	}
}
}